# coding:utf-8
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.preprocessing import MinMaxScaler

from utils import util

iter_index = 70 #How many data for train, 9 is the least.

# data_train, data_test = get_stock_data.process_data_by_tushare()
_, _, data = util.split_data()

data = data.sort_index(ascending=True, axis=0)[9600:]
data_label = data['label'].values

# Scale data
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler.fit(data)

correct = 0
train_original = iter_index
i=0
total_data_num=len(data)
pre_start = 570 # 预启动阶段

'''
#Classical classification, normal way
Data_train=Data[0:L-20]
value_train = value[0:L-20]
Data_predict=Data[L-20:L]
value_real = value[L-20:L]
print(Data_predict)
classifier = svm.SVC()
classifier.fit(Data_train,value_train)
value_predict=classifier.predict(Data_predict)
print("value_real = ",value_predict)
while i<19:
	print("value_real = ",value_real[i])
	if(value_real[i]==int(value_predict[i])):
		correct=correct+1
	i+=1
print("Correct = ",correct/19*100,"%")
'''
#loop training,15 days data for train
print("total_data_num: %s" % total_data_num)
while iter_index < total_data_num:
	train_batch = data[iter_index - train_original:iter_index]
	train_batch_label = data_label[iter_index-train_original:iter_index]

	# classifier = svm.SVC(kernel='poly',degree=40)#kernel='poly',(gamma*u'*v + coef0)^degree
	# classifier = svm.SVC(kernel='poly',degree=20) # 53%
	# classifier =svm.SVC(kernel='poly') #97% need optimization, some data may expand to infinite demension
	# classifier =svm.SVC(kernel='sigmoid') #49%
	# classifier =svm.SVC(kernel='precomputed') #bug
	classifier =svm.SVC() #kernel='rbf' %57
	# classifier = svm.LinearSVC()  # 69%

	classifier.fit(train_batch,train_batch_label)

	# 启动阶段不计算预测，经过一段时间的训练开始预测
	if (iter_index > pre_start):
		test_batch = data[iter_index:iter_index + 1]
		test_batch_label = data_label[iter_index:iter_index + 1]
		value_predict=classifier.predict(test_batch)

		if(test_batch_label[0]==int(value_predict)):
			correct=correct+1
	iter_index += 1
	if iter_index % 100 ==0:
		print("iteration step: %s, Correct = %s" % (iter_index, correct))

correct=float(correct)/float(total_data_num - pre_start)*100
print("Correct = %s" % correct)

'''
print("support_:",classifier.support_)
print("support_vectors_:",classifier.support_vectors_)
print("n_support_:",classifier.n_support_)
'''





